package com.example.s14_jpa2.service.impl;

import com.example.s14_jpa2.pojo.User;
import com.example.s14_jpa2.repository.UserRepository;
import com.example.s14_jpa2.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService
{
    @Autowired
    private UserRepository userRepository;

    @Override
    public Boolean save(User u)
    {
        return userRepository.save(u)!=null;
    }

    @Override
    public User findById(long id) {
        return userRepository.findById(id).get();
    }

    @Override
    public List<User> list()
    {
        return userRepository.findAll();
    }

    @Override
    public Page<User> list(Pageable pageable)
    {
        Page<User> page = userRepository.findAll(pageable);
        return page;
    }

    @Override
    public User findByUsername(String username)
    {
        User u = userRepository.findByUsername(username);
        return u;
    }

    @Override
    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }
}
